package futuramaParser;


import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.util.*;

/*
This program will read epidode .txt files and output them into a single txt file, parsed

episodes have to be named ep#.txt, and put into root of program directory

Have to use USER CONTROL commented section to change some global variables per season parse.
*/


public class parseEp1<E> extends ArrayList<E> { 
	

//            USER CONTROL                //
//which season are you on? (enter 1 for season 1)
static int seasonNumber=6;

//put dialog in parsed dialog data?
static boolean printDialog = true;

//Dont change. These are dynamic in main.
static String episode ="";
static String episodeName="";
static String fileOut = "";
static String wordsOut = "";
static String dialogOut = "";
//            USER CONTROL                //




















//list of characters to include in the parse
static String mainCharactersLoop[] = {"Fry", "Bender", "Leela", "Farnsworth", "Zoidberg", "Amy", "Hermes", "Nibbler",
	"Zapp", "Kif", "Mom", "Flexo", "Scruffy", "Wernstrom", "Mrs. Wong", "Mr. Wong", "Lrrr", "Calculon", "Robot Santa",
	"Elzar", "Smitty", "Donbot", "Clamps", "Nd-Nd", "Panucci", "Michelle", "Yancy", "Linda", "Morbo", "Joey", "Sal",
	"Cubert", "Dwight", "Liu", "Nixon", "Tinny Tim", "Preacherbot", "Melllvar", "URL", "Gus", "Nudar", "Schlump", 
	"Kwanzabot", "Al Gore", "Dr. Perceptron", "Hutch", "Nine"};
static ArrayList<String> mainCharacters = new ArrayList<String>();
//ipgee? roberto beck tate sheldon blek Uecker Mother Father (of leela s4e2) yivo Takei ignus larius (s5e12)
//hutch and nine are in into the wild green yonder


//FIX Robot Santa  inconsistency in s6e13 and Gore

//list of common words stored per episode
static ArrayList<String> words = new ArrayList<String>();

//lists of common words for all characters
static ArrayList<String> fryList = new ArrayList<String>();
static ArrayList<String> leelaList = new ArrayList<String>();
static ArrayList<String> benderList = new ArrayList<String>();
static ArrayList<String> farnsworthList = new ArrayList<String>();
static ArrayList<String> zoidbergList = new ArrayList<String>();
static ArrayList<String> amyList = new ArrayList<String>();
static ArrayList<String> hermesList = new ArrayList<String>();
static ArrayList<String> nibblerList = new ArrayList<String>();
static ArrayList<String> zappList = new ArrayList<String>();
static ArrayList<String> kifList = new ArrayList<String>();
static ArrayList<String> momList = new ArrayList<String>();
static ArrayList<String> flexoList = new ArrayList<String>();
static ArrayList<String> scruffyList = new ArrayList<String>();
static ArrayList<String> wernstromList = new ArrayList<String>();
static ArrayList<String> mrsWongList = new ArrayList<String>();
static ArrayList<String> mrWongList = new ArrayList<String>();
static ArrayList<String> lrrrList = new ArrayList<String>();
static ArrayList<String> calculonList = new ArrayList<String>();
static ArrayList<String> santaList = new ArrayList<String>();
static ArrayList<String> elzarList = new ArrayList<String>();
static ArrayList<String> smittyList = new ArrayList<String>();
static ArrayList<String> donbotList = new ArrayList<String>();
static ArrayList<String> clampsList = new ArrayList<String>();
static ArrayList<String> ndndList = new ArrayList<String>();
static ArrayList<String> panucciList = new ArrayList<String>();
static ArrayList<String> michelleList = new ArrayList<String>();
static ArrayList<String> yancyList = new ArrayList<String>();
static ArrayList<String> lindaList = new ArrayList<String>();
static ArrayList<String> morboList = new ArrayList<String>();
static ArrayList<String> joeyList = new ArrayList<String>();
static ArrayList<String> salList = new ArrayList<String>();
static ArrayList<String> cubertList = new ArrayList<String>();
static ArrayList<String> dwightList = new ArrayList<String>();
static ArrayList<String> liuList = new ArrayList<String>();
static ArrayList<String> nixonList = new ArrayList<String>();
static ArrayList<String> tinnyTimList = new ArrayList<String>();
static ArrayList<String> preacherbotList = new ArrayList<String>();
static ArrayList<String> melllvarList = new ArrayList<String>();
static ArrayList<String> urlList = new ArrayList<String>();
static ArrayList<String> gusList = new ArrayList<String>();
static ArrayList<String> nudarList = new ArrayList<String>();
static ArrayList<String> schlumpList = new ArrayList<String>();
static ArrayList<String> kwanzabotList = new ArrayList<String>();
static ArrayList<String> alGoreList = new ArrayList<String>();
static ArrayList<String> perceptronList = new ArrayList<String>();
static ArrayList<String> hutchList = new ArrayList<String>();
static ArrayList<String> nineList = new ArrayList<String>();

//Arrays of catch phrases	
static String zoidbergCatch[] = {"woop", "what an honour", "excellent", "i love that guy", "hooray", "dibs on"};
static String benderCatch[] = {"bite my", "me bender", "boned", "bender is great", "neat"};
static String farnsworthCatch[] = {"news everyone", "i am already in my pyjamas", "sweet zombie jesus", "huh", "wernstrom","fuff"};
static String fryCatch[] = {"help police", "he's dead", "ow my", "bender is that you"};
static String leelaCatch[] = {"hi-yah", "oh lord"};
static String hermesCatch[] = {"sweet", "my manwich"};

static int zoidbergInc[] = new int[6];
static int benderInc[] = new int[5];
static int farnsworthInc[] = new int[6];
static int fryInc[] = new int[4];
static int leelaInc[] = new int[2];
static int hermesInc[] = new int[2];	

    // This is private. It is not visible from outside.
    private Map<E,Integer> count = new HashMap<E,Integer>();

    // There are several entry points to this class
    // this is just to show one of them.
    public boolean add( E element  ) { 
        if( !count.containsKey( element ) ){
            count.put( element, 1 );
        } else { 
            count.put( element, count.get( element ) + 1 );
        }
        return super.add( element );
    }

    // This method belongs to CountItemList interface ( or class ) 
    // to used you have to cast.
    public int getCount( E element ) { 
        if( ! count.containsKey( element ) ) {
            return 0;
        }
        return count.get( element );
    }
	
	public static void main(String args[])
	{
		//change for loop limit to number of episodes in season+1
		if (seasonNumber ==1)
		{
			seasonNumber = 14;
			fileOut = "s1_out.txt";
			wordsOut = "s1_words.txt";
			dialogOut = "s1_dialogs.txt";
		}
		else if (seasonNumber ==2)
		{
			seasonNumber = 20;
			fileOut = "s2_out.txt";
			wordsOut = "s2_words.txt";
			dialogOut = "s2_dialogs.txt";
		}
		else if (seasonNumber ==3)
		{
			seasonNumber = 23;
			fileOut = "s3_out.txt";
			wordsOut = "s3_words.txt";
			dialogOut = "s3_dialogs.txt";
		}
		else if (seasonNumber ==4)
		{
			seasonNumber = 19;
			fileOut = "s4_out.txt";
			wordsOut = "s4_words.txt";
			dialogOut = "s4_dialogs.txt";
		}
		else if (seasonNumber ==5)
		{
			seasonNumber = 17;
			fileOut = "s5_out.txt";
			wordsOut = "s5_words.txt";
			dialogOut = "s5_dialogs.txt";
		}
		else if (seasonNumber ==6)
		{
			seasonNumber = 27;
			fileOut = "s6_out.txt";
			wordsOut = "s6_words.txt";
			dialogOut = "s6_dialogs.txt";
		}

		
		//extract and put common words into arraylist words
		getCommon();
		
		//put all the characters we will be parsing into an ArrayList for the nice contains() method
		for (int i =0; i <mainCharactersLoop.length; i++)
		{
			mainCharacters.add(mainCharactersLoop[i]);
		}

	try{
		  // Create file 
		  FileWriter fstream2 = new FileWriter(fileOut);
		  BufferedWriter out = new BufferedWriter(fstream2);
		  
		  FileWriter fstream4 = new FileWriter(dialogOut);
		  BufferedWriter out4 = new BufferedWriter(fstream4);
		  
		//loop through episodes... always +1 of last ep number
		  for (int e=1; e<seasonNumber; e++)
		  {
				String name = null, prevName = null, dialog = null, test="";
				boolean skip;
				List<String> charactersFULL = new parseEp1<String>();
				ArrayList<String> characters = new ArrayList<String>();
				String listOfChars ="";
				

				
				int lineCounter =0;
				
			  // Open the file that is the first 
			  episode = "ep" + e + ".txt";
			  FileInputStream fstream = new FileInputStream(episode);
			  
			  // Get the object of DataInputStream
			  DataInputStream in = new DataInputStream(fstream);
			  BufferedReader br = new BufferedReader(new InputStreamReader(in));
			  String strLine;
			  
			  //Read File Line By Line
			  while ((strLine = br.readLine()) != null)
			  {
				  //get episode name
				  if (lineCounter ==0)
				  {
					  episodeName = strLine;
					  out4.write("EPISODE\t" + episodeName + "\n");
				  }
				  
				  //count number of lines
				  lineCounter++;
				  
				  //remove time stamp
				  if (strLine.startsWith("("))
					  strLine = strLine.substring(7, strLine.length());

				  
				  StringTokenizer st = new StringTokenizer(strLine);
				  
				  //reset skip variable to false
				  skip = false;
				  
				  //get character name, they never begin with [
				  if (!strLine.startsWith("["))
				  {
					  //before getting character's name, first must check if the character is singing
					  //these lines do not have a name in them.. so no ":"
					  if (!strLine.contains(":") && lineCounter !=1)
					  {
						  name = prevName;
					  }
					  else
					  {
						  //this code will make a character's name that has more than 1 word work
						  name = st.nextToken();
	
						  if (!name.endsWith(":"))
						  {
							  while(st.hasMoreTokens())
							  {
								if (name.endsWith(":"))
								{
									break;
								}
								 name +=  " " + st.nextToken();
							  }
						  }
	
						  //remove the colon at end of name and store name in prevName
						  if (name.endsWith(":"))
						  {
							  name = name.substring(0, name.length()-1);
							  
							  //fixing an inconsistency in the script
							  if (name.equals("Professor Farnsworth") || name.equals("Prof. Farnsworth"))
								  name = "Farnsworth";
							  if (name.equals("Al Gore's head") || name.equals("Gore"))
								  name = "Al Gore";
							  if (name.equals("Santa"))
								  name = "Robot Santa";
							  
							  prevName = name;
						  }
						  //remove (xxxx) at the end of the name if it exists ... seen in s6ep1
						  if (name.endsWith(")"))
						  {
							  int start = name.indexOf("(");
							  name = name.substring(0, start);
							  name = name.trim();
							  
							  //fixing an inconsistency in the script
							  if (name.equals("Professor Farnsworth")|| name.equals("Prof. Farnsworth"))
								  name = "Farnsworth";
							  if (name.equals("Al Gore's head") || name.equals("Gore"))
								  name = "Al Gore";
							  if (name.equals("Santa"))
								  name = "Robot Santa";
							  
							  prevName = name;
						  }
					  }
				  }
				  else
					skip = true;
				  
				  //skip if name is not a common character
				  if (!mainCharacters.contains(name))
					  skip = true;
				  
				  //list of all unique characters in script
				  if (!skip)
					  if (!characters.contains(name))
						  characters.add(name);
				 
				  //list of all characters in script, including duplicates
				  if (!skip)
					  charactersFULL.add(name);
				  
				  
				  //reset dialog and test variables to empty
				  dialog = "";
				  test = "";
				  

				  //parse the dialog
				  if (!skip)
				  {
					  while (st.hasMoreTokens())
					  {
						  test = st.nextToken();
						  test = test.toLowerCase();
						  if (test.startsWith("[") && test.endsWith("]"))
						  {
							  //do nothing
						  }
						  else if (test.startsWith("["))
						  {
							  while (st.hasMoreTokens())
							  {
								  test = st.nextToken();
								  if (test.endsWith("]"))
								  {
									  break;
								  }
							  }
						  }
						  else
						  {
							  //have a token from a character's dialog
							  //put into List if it is not a 'common word'
							  test=test.trim();
							  test = removePunct(test);
							  if (name.equals("Fry") && !words.contains(test.toLowerCase()))
								  fryList.add(test);
							  else if (name.equals("Leela") && !words.contains(test.toLowerCase()))
								  leelaList.add(test);
							  else if (name.equals("Bender") && !words.contains(test.toLowerCase()))
								  benderList.add(test);
							  else if (name.equals("Farnsworth") && !words.contains(test.toLowerCase()))
								  farnsworthList.add(test);
							  else if (name.equals("Zoidberg") && !words.contains(test.toLowerCase()))
								  zoidbergList.add(test);
							  else if (name.equals("Amy") && !words.contains(test.toLowerCase()))
								  amyList.add(test);
							  else if (name.equals("Hermes") && !words.contains(test.toLowerCase()))
								  hermesList.add(test);
							  else if (name.equals("Nibbler") && !words.contains(test.toLowerCase()))
								  nibblerList.add(test);
							  else if (name.equals("Zapp") && !words.contains(test.toLowerCase()))
								  zappList.add(test);
							  else if (name.equals("Kif") && !words.contains(test.toLowerCase()))
								  kifList.add(test);
							  else if (name.equals("Mom") && !words.contains(test.toLowerCase()))
								  momList.add(test);
							  else if (name.equals("Flexo") && !words.contains(test.toLowerCase()))
								  flexoList.add(test);
							  else if (name.equals("Scruffy") && !words.contains(test.toLowerCase()))
								  scruffyList.add(test);
							  else if (name.equals("Wernstrom") && !words.contains(test.toLowerCase()))
								  wernstromList.add(test);
							  else if (name.equals("Mrs. Wong") && !words.contains(test.toLowerCase()))
								  mrsWongList.add(test);
							  else if (name.equals("Mr. Wong") && !words.contains(test.toLowerCase()))
								  mrWongList.add(test);
							  else if (name.equals("Lrrr") && !words.contains(test.toLowerCase()))
								  lrrrList.add(test);
							  else if (name.equals("Calculon") && !words.contains(test.toLowerCase()))
								  calculonList.add(test);
							  else if (name.equals("Robot Santa") && !words.contains(test.toLowerCase()))
								  santaList.add(test);
							  else if (name.equals("Elzar") && !words.contains(test.toLowerCase()))
								  elzarList.add(test);
							  else if (name.equals("Smitty") && !words.contains(test.toLowerCase()))
								  smittyList.add(test);
							  else if (name.equals("Donbot") && !words.contains(test.toLowerCase()))
								  donbotList.add(test);
							  else if (name.equals("Clamps") && !words.contains(test.toLowerCase()))
								  clampsList.add(test);
							  else if (name.equals("Nd-Nd") && !words.contains(test.toLowerCase()))
								  ndndList.add(test);
							  else if (name.equals("Panucci") && !words.contains(test.toLowerCase()))
								  panucciList.add(test);
							  else if (name.equals("Michelle") && !words.contains(test.toLowerCase()))
								  michelleList.add(test);
							  else if (name.equals("Yancy") && !words.contains(test.toLowerCase()))
								  yancyList.add(test);
							  else if (name.equals("Linda") && !words.contains(test.toLowerCase()))
								  lindaList.add(test);
							  else if (name.equals("Morbo") && !words.contains(test.toLowerCase()))
								  morboList.add(test);
							  else if (name.equals("Joey") && !words.contains(test.toLowerCase()))
								  joeyList.add(test);
							  else if (name.equals("Sal") && !words.contains(test.toLowerCase()))
								  salList.add(test);
							  else if (name.equals("Cubert") && !words.contains(test.toLowerCase()))
								  cubertList.add(test);
							  else if (name.equals("Dwight") && !words.contains(test.toLowerCase()))
								  dwightList.add(test);
							  else if (name.equals("Liu") && !words.contains(test.toLowerCase()))
								  liuList.add(test);
							  else if (name.equals("Nixon") && !words.contains(test.toLowerCase()))
								  nixonList.add(test);
							  else if (name.equals("Tinny Tim") && !words.contains(test.toLowerCase()))
								  tinnyTimList.add(test);
							  else if (name.equals("Preacherbot") && !words.contains(test.toLowerCase()))
								  preacherbotList.add(test);
							  else if (name.equals("Melllvar") && !words.contains(test.toLowerCase()))
								  melllvarList.add(test);
							  else if (name.equals("URL") && !words.contains(test.toLowerCase()))
								  urlList.add(test);
							  else if (name.equals("Gus") && !words.contains(test.toLowerCase()))
								  gusList.add(test);
							  else if (name.equals("Nudar") && !words.contains(test.toLowerCase()))
								  nudarList.add(test);
							  else if (name.equals("Schlump") && !words.contains(test.toLowerCase()))
								  schlumpList.add(test);
							  else if (name.equals("Kwanzabot") && !words.contains(test.toLowerCase()))
								  kwanzabotList.add(test);
							  else if (name.equals("Al Gore") && !words.contains(test.toLowerCase()))
								  alGoreList.add(test);
							  else if (name.equals("Dr. Perceptron") && !words.contains(test.toLowerCase()))
								  perceptronList.add(test);
							  else if (name.equals("Hutch") && !words.contains(test.toLowerCase()))
								  hutchList.add(test);
							  else if (name.equals("Nine") && !words.contains(test.toLowerCase()))
								  nineList.add(test);
							  
							  dialog += test + " ";
						  }
					  }
				  dialog = dialog.trim();
				  }
				  
				  //write to file
				  try
				  {
					  //only write dialog to file. Skip boolean flagged transitional scenes
					  	if (!skip)
					  	{
					  		
					  		//checking for catch phrase and incrementing
					  		if (name.equals("Zoidberg"))
					  		{
					  			for (int i=0; i<zoidbergCatch.length; i++)
					  			{
					  				if (dialog.toLowerCase().contains(zoidbergCatch[i]))
					  				{
					  					zoidbergInc[i]++;
					  				}
					  			}
					  		}
					  		else if (name.equals("Bender"))
					  		{
					  			for (int i=0; i<benderCatch.length; i++)
					  			{
					  				if (dialog.toLowerCase().contains(benderCatch[i]))
					  				{
					  					benderInc[i]++;
					  				}
					  			}
					  		}
					  		else if (name.equals("Farnsworth"))
					  		{
					  			for (int i=0; i<farnsworthCatch.length; i++)
					  			{
					  				if (dialog.toLowerCase().contains(farnsworthCatch[i]))
					  				{
					  					farnsworthInc[i]++;
					  				}
					  			}
					  		}
					  		else if (name.equals("Fry"))
					  		{
					  			for (int i=0; i<fryCatch.length; i++)
					  			{
					  				if (dialog.toLowerCase().contains(fryCatch[i]))
					  				{
					  					fryInc[i]++;
					  				}
					  			}
					  		}
					  		else if (name.equals("Leela"))
					  		{
					  			for (int i=0; i<leelaCatch.length; i++)
					  			{
					  				if (dialog.toLowerCase().contains(leelaCatch[i]))
					  				{
					  					leelaInc[i]++;
					  				}
					  			}
					  		}
					  		else if (name.equals("Hermes"))
					  		{
					  			for (int i=0; i<hermesCatch.length; i++)
					  			{
					  				if (dialog.toLowerCase().contains(hermesCatch[i]))
					  				{
					  					hermesInc[i]++;
					  				}
					  			}
					  		}
					  		
					  		if (printDialog)
					  			out4.write(name + '\t' + "{" + dialog + "}" + '\n');
					  				
					  	}

				  }
				  catch (Exception ex)
				  {
					  System.err.println("Error: " + ex.getMessage());
				  }
			  }//end while loop
			  
			  //at the end of write to file, include list of characters/how many lines they said/catch phrases they said
			  for (int i=0; i<characters.size(); i++)
			  {
				  //we only want to print common characters throughout the series
				  /*
				  boolean printName = false;
				  if (characters.get(i).equals("Fry") || characters.get(i).equals("Leela"))
					  printName = true;
				  
				  if (printName)
				  {*/
					 listOfChars += characters.get(i) + "\t" + (( parseEp1<String> )charactersFULL).getCount( characters.get(i) )+" ";
					  
					if (characters.get(i).equals("Zoidberg"))
					  for (int j=0;j<zoidbergInc.length; j++)
						  listOfChars += (zoidbergInc[j] + " ");
				  else if (characters.get(i).equals("Bender"))
					  for (int j=0;j<benderInc.length; j++)
						  listOfChars += (benderInc[j] + " ");
				  else if (characters.get(i).equals("Farnsworth"))
					  for (int j=0;j<farnsworthInc.length; j++)
						  listOfChars += (farnsworthInc[j] + " ");
				  else if (characters.get(i).equals("Fry"))
					  for (int j=0;j<fryInc.length; j++)
						  listOfChars += (fryInc[j] + " ");
				  else if (characters.get(i).equals("Leela"))
					  for (int j=0;j<leelaInc.length; j++)
						  listOfChars += (leelaInc[j] + " ");
				  else if (characters.get(i).equals("Hermes"))
					  for (int j=0;j<hermesInc.length; j++)
						  listOfChars += (hermesInc[j] + " ");  
				  
					listOfChars += "\n";
				 // }
				  
					  
			}
			try
			{
				//at end of an episode parse, include character information
				out.write("EPISODE\t" + episodeName + "\n");
			  	out.write(listOfChars);
			  	out.write("LIST_OF_CHARACTERS_OVER\n");
			}
			catch (Exception ex)
			{
				System.err.println("Error: " + ex.getMessage());
			}
			  
			//done reading file, close input stream
			in.close();
			
			//begin checking for uncommon words
			//sort the arrays
			sortAllArrays();

			try
			{
				// Create file 
				FileWriter fstream3 = new FileWriter(wordsOut, true);
				BufferedWriter out3 = new BufferedWriter(fstream3);
				out3.write("****************\n" + episodeName + "\n****************\n");
			  	out3.close();
			}
			catch (Exception ex)
			{
				System.err.println("Error: " + ex.getMessage());
			}
			
			//get top3 words
			getAllTopThree();
			
			
			try
			{
				// Create file 
				FileWriter fstream3 = new FileWriter(wordsOut, true);
				BufferedWriter out3 = new BufferedWriter(fstream3);
				out3.write("****************\n" + "END" + "\n****************\n");
			  	out3.close();
			}
			catch (Exception ex)
			{
				System.err.println("Error: " + ex.getMessage());
			}
			
			
			clearAll();
		  }//end episode for loop
		
		  //done looping through files, close output stream
		  out.close();
		  out4.close();
		}
		catch (Exception ex)
		{
			System.err.println("Error: " + ex.getMessage());
		}
	}//main loop
	
	
	//make array of common words
	static void getCommon()
	{
		//ArrayList<String> words = new ArrayList<String>();
		String word ="";
		
		try
		{
			FileInputStream fstream = new FileInputStream("commonWords.txt");
				  
			// Get the object of DataInputStream
			DataInputStream in = new DataInputStream(fstream);
			BufferedReader br = new BufferedReader(new InputStreamReader(in));
			String strLine;
			
			while ((strLine = br.readLine()) != null)
			{
				StringTokenizer st = new StringTokenizer(strLine);
				
				if (st.hasMoreTokens())
				{
					word = st.nextToken();
					word = word.toLowerCase();
					if (word.startsWith("*"))
						break;
					
					words.add(word);
				}
			}
			
			System.out.println(words);
			
			
		}
		catch(Exception e)
		{
			System.out.println ("Error: " + e);
		}
	}
	
	static String removePunct(String word)
	{
		word = word.trim();
		
		if (word.endsWith(","))
		{
			word = word.substring(0, word.length()-1);
		}
		else if (word.endsWith("."))
		{
			word = word.substring(0, word.length()-1);
		}
		else if (word.endsWith("!"))
		{
			word = word.substring(0, word.length()-1);
		}
		else if (word.endsWith("?"))
		{
			word = word.substring(0, word.length()-1);
		}
		else if (word.endsWith(":"))
		{
			word = word.substring(0, word.length()-1);
		}
		else if (word.endsWith("\""))
		{
			word = word.substring(0, word.length()-1);
		}
		else if (word.endsWith(";"))
		{
			word = word.substring(0, word.length()-1);
		}
		else if (word.endsWith(")"))
		{
			word = word.substring(0, word.length()-1);
		}
		else if (word.startsWith("\""))
		{
			word = word.substring(1, word.length());
		}
		else if (word.startsWith("("))
		{
			word = word.substring(1, word.length());
		}
		return word;
	}
	
	//finds top three uncommon words for the episode for a character
	static void getTopThree(ArrayList<String> theArrayFULL, String character)
	{
		String test ="", test2="", test3="", test4="", test5="", test6="", test7="", test8="", test9="", test10="";
		
		List<String> theArrayFullTEST = new parseEp1<String>();
		
		for (int i=0; i < theArrayFULL.size(); i++)
		{
			theArrayFullTEST.add(theArrayFULL.get(i));
		}
		
		
		ArrayList<String> theArray = new ArrayList<String>();
		ArrayList<Integer> counter = new ArrayList<Integer>();

		
		
		for (int i=0; i < theArrayFULL.size(); i++)
		{
			  if (!theArray.contains(theArrayFULL.get(i)))
				  theArray.add(theArrayFULL.get(i));
		}
		
		for (int i=0; i < theArray.size(); i++)
		{
			counter.add(((parseEp1<String>) theArrayFullTEST).getCount( theArray.get(i) ));
		}
		
		int one=0, two=0, three=0, four=0, five=0, six=0, seven=0, eight=0, nine=0, ten=0;
		int ione=-1, itwo=-1, ithree=-1, ifour=-1, ifive=-1, isix=-1, iseven=-1, ieight=-1, inine=-1, iten=-1;
		
		

		//gets the most common word, puts it into test
		for (int i=0; i < counter.size(); i++)
		{
			if (counter.get(i) > one)
			{
			one = counter.get(i);
			test = theArray.get(i);
			ione = i;
			}
		}

		//gets second most common word, puts into test2
		for (int i=0; i < counter.size(); i++)
		{
			if (i != ione)
			{
				if (counter.get(i) > two)
				{
					two = counter.get(i);
					test2 = theArray.get(i);
					itwo = i;
				}
			}
		}
		
		//third
		for (int i=0; i < counter.size(); i++)
		{
			if (i != ione && i != itwo)
			{
				if (counter.get(i) > three)
				{
					three = counter.get(i);
					test3 = theArray.get(i);
					ithree = i;
				}
			}
		}
		
		//fourth
		for (int i=0; i < counter.size(); i++)
		{
			if (i != ione && i != itwo && i != ithree)
			{
				if (counter.get(i) > four)
				{
					four = counter.get(i);
					test4 = theArray.get(i);
					ifour = i;
				}
			}
		}
		
		//fifth
		for (int i=0; i < counter.size(); i++)
		{
			if (i != ione && i != itwo && i != ithree && i != ifour)
			{
				if (counter.get(i) > five)
				{
					five = counter.get(i);
					test5 = theArray.get(i);
					ifive = i;
				}
			}
		}
		
		//sixth
		for (int i=0; i < counter.size(); i++)
		{
			if (i != ione && i != itwo && i != ithree && i != ifour && i != ifive)
			{
				if (counter.get(i) > six)
				{
					six = counter.get(i);
					test6 = theArray.get(i);
					isix = i;
				}
			}
		}
		
		//seventh
		for (int i=0; i < counter.size(); i++)
		{
			if (i != ione && i != itwo && i != ithree && i != ifour && i != ifive && i != isix)
			{
				if (counter.get(i) > seven)
				{
					seven = counter.get(i);
					test7 = theArray.get(i);
					iseven = i;
				}
			}
		}

		//eighth
		for (int i=0; i < counter.size(); i++)
		{
			if (i != ione && i != itwo && i != ithree && i != ifour && i != ifive && i != isix && i != iseven)
			{
				if (counter.get(i) > eight)
				{
					eight = counter.get(i);
					test8 = theArray.get(i);
					ieight = i;
				}
			}
		}
		
		//ninth
		for (int i=0; i < counter.size(); i++)
		{
			if (i != ione && i != itwo && i != ithree && i != ifour && i != ifive && i != isix && i != iseven && i != ieight)
			{
				if (counter.get(i) > nine)
				{
					nine = counter.get(i);
					test9 = theArray.get(i);
					inine = i;
				}
			}
		}
		
		//ten
		for (int i=0; i < counter.size(); i++)
		{
			if (i != ione && i != itwo && i != ithree && i != ifour && i != ifive && i != isix && i != iseven && i != ieight && i != inine)
			{
				if (counter.get(i) > ten)
				{
					ten = counter.get(i);
					test10 = theArray.get(i);
					iten = i;
				}
			}
		}
		
		try
		{
			System.out.println ("Printing: " + episode);
			// Create file 
			FileWriter fstream2 = new FileWriter(wordsOut, true);
			BufferedWriter out = new BufferedWriter(fstream2);
			  
			//at end of an episode parse, include character information
			if (counter.size() != 0)
			{
				out.write(character +"\t");
				//out.write(counter.toString());
				
				if (one!=0)
					out.write(one + "\t" + test + "\t");
				if (two!=0)
					out.write(two + "\t" + test2 + "\t");
				if (three!=0)
					out.write(three + "\t" + test3 + "\t");
				if (four!=0)
					out.write(four + "\t" + test4 + "\t");
				if (five!=0)
					out.write(five + "\t" + test5 + "\t");
				if (six!=0)
					out.write(six + "\t" + test6 + "\t");
				if (seven!=0)
					out.write(seven + "\t" + test7 + "\t");
				if (eight!=0)
					out.write(eight + "\t" + test8 + "\t");
				if (nine!=0)
					out.write(nine + "\t" + test9 + "\t");
				if (ten!=0)
					out.write(ten + "\t" + test10);

				out.write("\n");
				//out.write(counter.toString() + " 1: " + one + " with: "+ test + " 2: "+ two +" with: " + test2+ " 3: "+ three +" with: " + test3+"\n");
			}
		  	out.close();
		}
		catch (Exception ex)
		{
			System.err.println("Error: " + ex.getMessage());
		}
		
	}
	
	static void clearAll()
	{
		fryList.clear();
		leelaList.clear();
		benderList.clear();
		farnsworthList.clear();
		zoidbergList.clear();
		amyList.clear();
		hermesList.clear();
		nibblerList.clear();
		zappList.clear();
		kifList.clear();
		momList.clear();
		flexoList.clear();
		scruffyList.clear();
		wernstromList.clear();
		mrsWongList.clear();
		mrWongList.clear();
		lrrrList.clear();
		calculonList.clear();
		santaList.clear();
		elzarList.clear();
		smittyList.clear();
		donbotList.clear();
		clampsList.clear();
		ndndList.clear();
		panucciList.clear();
		michelleList.clear();
		yancyList.clear();
		lindaList.clear();
		morboList.clear();
		joeyList.clear();
		salList.clear();
		cubertList.clear();
		dwightList.clear();
		liuList.clear();
		nixonList.clear();
		tinnyTimList.clear();
		preacherbotList.clear();
		melllvarList.clear();
		urlList.clear();
		gusList.clear();
		nudarList.clear();
		schlumpList.clear();
		kwanzabotList.clear();
		alGoreList.clear();
		perceptronList.clear();
		hutchList.clear();
		nineList.clear();
		
		zoidbergInc = new int[6];
		benderInc = new int[5];
		farnsworthInc = new int[6];
		fryInc = new int[4];
		leelaInc = new int[2];
		hermesInc = new int[2];	
	}
	
	static void sortAllArrays()
	{
		Collections.sort(fryList);
		Collections.sort(leelaList);
		Collections.sort(benderList);
		Collections.sort(farnsworthList);
		Collections.sort(zoidbergList);
		Collections.sort(amyList);
		Collections.sort(hermesList);
		Collections.sort(nibblerList);
		Collections.sort(zappList);
		Collections.sort(kifList);
		Collections.sort(momList);
		Collections.sort(flexoList);
		Collections.sort(scruffyList);
		Collections.sort(wernstromList);
		Collections.sort(mrsWongList);
		Collections.sort(mrWongList);
		Collections.sort(lrrrList);
		Collections.sort(calculonList);
		Collections.sort(santaList);
		Collections.sort(elzarList);
		Collections.sort(smittyList);
		Collections.sort(donbotList);
		Collections.sort(clampsList);
		Collections.sort(ndndList);
		Collections.sort(panucciList);
		Collections.sort(michelleList);
		Collections.sort(yancyList);
		Collections.sort(lindaList);
		Collections.sort(morboList);
		Collections.sort(joeyList);
		Collections.sort(salList);
		Collections.sort(cubertList);
		Collections.sort(dwightList);
		Collections.sort(liuList);
		Collections.sort(nixonList);
		Collections.sort(tinnyTimList);
		Collections.sort(preacherbotList);
		Collections.sort(melllvarList);
		Collections.sort(urlList);
		Collections.sort(gusList);
		Collections.sort(nudarList);
		Collections.sort(schlumpList);
		Collections.sort(kwanzabotList);
		Collections.sort(alGoreList);
		Collections.sort(perceptronList);
		Collections.sort(hutchList);
		Collections.sort(nineList);
	}
	
	static void getAllTopThree()
	{
		getTopThree(fryList, "Fry");
		getTopThree(leelaList, "Leela");
		getTopThree(benderList, "Bender");
		getTopThree(farnsworthList, "Farnsworth");
		getTopThree(zoidbergList, "Zoidberg");
		getTopThree(amyList, "Amy");
		getTopThree(hermesList, "Hermes");
		getTopThree(nibblerList, "Nibbler");
		getTopThree(zappList, "Zapp");
		getTopThree(kifList, "Kif");
		getTopThree(momList, "Mom");
		getTopThree(flexoList, "Flexo");
		getTopThree(scruffyList, "Scruffy");
		getTopThree(wernstromList, "Wernstrom");
		getTopThree(mrsWongList, "Mrs. Wong");
		getTopThree(mrWongList, "Mr. Wong");
		getTopThree(lrrrList, "Lrrr");
		getTopThree(calculonList, "Calculon");
		getTopThree(santaList, "Robot Santa");
		getTopThree(elzarList, "Elzar");
		getTopThree(smittyList, "Smitty");
		getTopThree(donbotList, "Donbot");
		getTopThree(clampsList, "Clamps");
		getTopThree(ndndList, "Nd-Nd");
		getTopThree(panucciList, "Panucci");
		getTopThree(michelleList, "Michelle");
		getTopThree(yancyList, "Yancy");
		getTopThree(lindaList, "Linda");
		getTopThree(morboList, "Morbo");
		getTopThree(joeyList, "Joey");
		getTopThree(salList, "Sal");
		getTopThree(cubertList, "Cubert");
		getTopThree(dwightList, "Dwight");
		getTopThree(liuList, "Liu");
		getTopThree(nixonList, "Nixon");
		getTopThree(tinnyTimList, "Tinny Tim");
		getTopThree(preacherbotList, "Preacherbot");
		getTopThree(melllvarList, "Melllvar");
		getTopThree(urlList, "URL");
		getTopThree(gusList, "Gus");
		getTopThree(nudarList, "Nudar");
		getTopThree(schlumpList, "Schlump");
		getTopThree(kwanzabotList, "Kwanzabot");
		getTopThree(alGoreList, "Al Gore");
		getTopThree(perceptronList, "Dr. Perceptron");
		getTopThree(hutchList, "Hutch");
		getTopThree(nineList, "Nine");
	}
}//class loop
		  